﻿using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace Cluster
{
    public abstract partial class ClusterManager
    {
        /// <summary>
        /// Serializes an object to buffer
        /// </summary>
        /// <param name="o"></param>
        /// <returns></returns>
        public static byte[] Serialize(object o)
        {
            using (var ms = new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(ms, o);
                return ms.GetBuffer();
            }
        }

        /// <summary>
        /// Deserializes an object from a buffer
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static T Deserialize<T>(byte[] buffer)
        {
            using (var ms = new MemoryStream(buffer))
            {
                var formatter = new BinaryFormatter();
                return (T) formatter.Deserialize(ms);
            }
        }
    }
}
